﻿package smooth.core.object.desktopComponent {
	import app.core.action.RotatableScalable;
	import caurina.transitions.Tweener;
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.geom.Point;
	import flash.events.*;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.AntiAliasType;
	import smooth.core.object.desktopCanvasComponent.DesktopCanvasComponent;
	
	public class DesktopComponent extends RotatableScalable {

		[Embed(source="../../../../../resources/fonts/Arial.ttf", fontFamily="myFont")] 
		public var myFont:Class;
		
		public function DesktopComponent():void {
			// Init des visuels du composant			
			initGraphics();
			// Init des events du composant
			initEvents();			
			// Init des process du composant
			initProcess();
		}

		private function initGraphics():void {
			// Formatage du nom du composant
			var formatLabel:TextFormat= new TextFormat();
			formatLabel.font= "myFont";
			formatLabel.color= 0xFFFFFF;
			formatLabel.size= 15;
			// Nom du composant
			var tLabel: TextField;
			tLabel = new TextField();
			tLabel.embedFonts = true;
			tLabel.selectable  = false;		 
			tLabel.text = "Desktop";
			tLabel.autoSize = TextFieldAutoSize.CENTER;
			tLabel.name = "tLabel";
			tLabel.x = 0;
			tLabel.y = 0;
			tLabel.setTextFormat(formatLabel);
			// Formatage du libellé de recherche
			var formatSearchLabel:TextFormat= new TextFormat();
			formatSearchLabel.font= "myFont";
			formatSearchLabel.color= 0xFFFFFF;
			formatSearchLabel.size= 15;
			// Nom du composant
			var tSearch: TextField;
			tSearch = new TextField();
			tSearch.embedFonts = true;
			tSearch.selectable  = false;		 
			tSearch.text = "";
			tSearch.autoSize = TextFieldAutoSize.CENTER;
			tSearch.name = "tSearch";
			tSearch.x = 0;
			tSearch.y = 50;
			tSearch.visible = false;
			tSearch.setTextFormat(formatSearchLabel);
			// Construction visuelle du composant
			this.graphics.lineStyle(1.0,0xFFFFFF,1.0);
			this.graphics.beginFill(0xFFFFFF,0.75);
			this.graphics.drawCircle(0, 0, 34);
			this.graphics.beginFill(0x0F0F0F,0.75);
			this.graphics.drawCircle(0, 0, 47);
			this.graphics.endFill();
			this.addChild(tLabel);
			this.addChild(tSearch);
		}
		
		private function initProcess():void {
		}
		
		private function initEvents():void {
			addEventListener(TouchEvent.LONG_PRESS, onTouchLongPress, false, 0, true);
		}
		
		public function doInitSearchLabel(searchLabel:String):void {
			var formatSearchLabel:TextFormat= new TextFormat();
			formatSearchLabel.font= "myFont";
			formatSearchLabel.color= 0xFFFFFF;
			formatSearchLabel.size = 15;
			var tSearch:TextField = this.getChildByName("tSearch") as TextField
			tSearch.text = searchLabel;
			tSearch.visible = true;
			tSearch.setTextFormat(formatSearchLabel);
		}
		
		public function doAnimationFadeOut():void {
			Tweener.addTween(this, { alpha:0.2, time:2, transition:"linear" } );	
		}
		
		public function doAnimationFadeIn():void {
			this.alpha = 0;
			Tweener.addTween(this, { alpha:1, time:2, transition:"linear" } );	
		}
		
		private function onTouchLongPress(e:TouchEvent):void {
			var desktopCanvas1:DesktopCanvasComponent = this.doCreateDesktopCanvasComponent(200, 200, 800, 800, 0x000000, 0.75);
		}
		
		private function doCreateDesktopCanvasComponent(posX:int, posY:int, sizeX:int, sizeY:int, color:int, alpha:Number):DesktopCanvasComponent {
			var desktopCanvas1:DesktopCanvasComponent = new DesktopCanvasComponent(posX, posY, sizeX, sizeY, color, alpha);
			this.parent.addChild(desktopCanvas1);
			return desktopCanvas1;
		}
		
	}
}